import { Form, message } from 'antd';
import React, { useEffect } from 'react';
import AddDirModal from './addDirModal';
import { editContentDir } from '@/services/contentMng/contentDirectory';

interface IProps {
  open: boolean;
  onClose: (options?: { isRefresh: boolean }) => void;
  data: any;
}

const AddFirstLevelDir = (props: IProps) => {
  const { open, onClose, data } = props;
  const [form] = Form.useForm();
  useEffect(() => {
    if (!data) return;

    form?.setFieldsValue({
      contents: data.contents,
      catalogueName: data.catalogueName,
    });
  }, [data]);

  const onCancel = () => {
    form.resetFields();
    onClose();
  };

  const onSave = async () => {
    let values = await form.validateFields();
    let params = {
      sid: data.sid,
      catalogueName: values.catalogueName,
      contents: values.contents,
    };

    let result = await editContentDir(params);
    if (result.statusCode === '0') {
      form.resetFields();
      onClose({ isRefresh: true });
      message.success('编辑成功');
    }
  };

  return (
    <AddDirModal
      title="编辑目录"
      open={open}
      onCancel={onCancel}
      onOk={onSave}
      formProps={{ form }}
      fieldsList={data?.contents || []}
    />
  );
};

export default AddFirstLevelDir;
